<template>
  <el-main>
    <router-view v-if="isRouterShow"> </router-view>
  </el-main>
  <el-footer v-show="footer">
    <Footer />
  </el-footer>
</template>

<script setup>
import { ref, onBeforeUnmount, provide, watch } from 'vue'
import { storeToRefs } from 'pinia'
import { useDebounceFn } from '@vueuse/core'
import { useGlobalStore } from '@/stores/modules/global'
import Footer from '@/layouts/components/Footer/index.vue'

const globalStore = useGlobalStore()
const { isCollapse, layout, footer } = storeToRefs(globalStore)

// 注入刷新页面方法
const isRouterShow = ref(true)
const refreshCurrentPage = val => (isRouterShow.value = val)
provide('refresh', refreshCurrentPage)

// 监听布局变化，在 body 上添加相对应的 layout class
watch(
  () => layout.value,
  () => {
    const body = document.body
    body.setAttribute('class', layout.value)
  },
  { immediate: true }
)

// 监听窗口大小变化，折叠侧边栏
const screenWidth = ref(0)
const listeningWindow = useDebounceFn(() => {
  screenWidth.value = document.body.clientWidth
  if (!isCollapse.value && screenWidth.value < 1200)
    globalStore.setGlobalState('isCollapse', true)
  if (isCollapse.value && screenWidth.value > 1200)
    globalStore.setGlobalState('isCollapse', false)
}, 100)
window.addEventListener('resize', listeningWindow, false)
onBeforeUnmount(() => {
  window.removeEventListener('resize', listeningWindow)
})
</script>

<style scoped lang="scss">
@import './index.scss';
</style>
